package com.inspur.demo.core.exception;

import com.inspur.demo.core.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.io.IOException;
import java.util.Set;

/**
 * @ClassName GlobalExceptionHandler
 * @Description TODO
 * @Company inspur
 * @Author Mark.Zhang
 * @Date 2019/5/17 14:36
 * @Version 1.0 
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    //校验异常
    @ExceptionHandler(ValidationException.class)
    public Result ServiceExceptionHandler(ValidationException e) {
        log.error(e.toString(), e);
        return Result.error(400, e.getMessage());
    }

    //校验异常
    @ExceptionHandler(ConstraintViolationException.class)
    public Result ServiceExceptionHandler(ConstraintViolationException e) {
        StringBuffer sb = new StringBuffer();
        ConstraintViolationException exs = (ConstraintViolationException) e;
        Set<ConstraintViolation<?>> violations = exs.getConstraintViolations();
        for (ConstraintViolation<?> item : violations) {
            /**打印验证不通过的信息*/
            System.out.println(item.getMessage());
            sb.append(item.getMessage());
        }
        log.error(sb.toString());
        return Result.error(400, sb.toString());
    }

    //业务层异常
//    @ExceptionHandler(BusinessException.class)
//    public Result ServiceExceptionHandler(BusinessException e) {
//        log.error(e.getMessage(), e);
//        return Result.error(e.getErrCode(), e.getMessage());
//    }

    //控制层异常
//    @ExceptionHandler(ControllerException.class)
//    public Result ControllerExceptionHandler(ControllerException e) {
//        log.error(e.getMessage(), e);
//        return Result.error(e.getErrCode(), e.getMessage());
//    }


    //空指针异常
    @ExceptionHandler(NullPointerException.class)
    public Result nullPointerExceptionHandler(NullPointerException e) {
        log.error(e.getMessage(), e);
        return Result.error(400, e.getMessage());
    }


    //类型转换异常
    @ExceptionHandler(ClassCastException.class)
    public Result classCastExceptionHandler(ClassCastException e) {
        log.error(e.getMessage(), e);
        return Result.error(400, e.getMessage());
    }

    //IO异常
    @ExceptionHandler(IOException.class)
    public Result iOExceptionHandler(IOException e) {
        log.error(e.getMessage(), e);
        return Result.error(400, e.getMessage());
    }

    //未知方法异常
    @ExceptionHandler(NoSuchMethodException.class)
    public Result noSuchMethodExceptionHandler(NoSuchMethodException e) {
        log.error(e.getMessage(), e);
        return Result.error(400, e.getMessage());
    }

    //数组越界异常
    @ExceptionHandler(IndexOutOfBoundsException.class)
    public Result indexOutOfBoundsExceptionHandler(IndexOutOfBoundsException e) {
        log.error(e.getMessage(), e);
        return Result.error(400, e.getMessage());
    }

    //400错误
    @ExceptionHandler({HttpMessageNotReadableException.class})
    public Result requestNotReadable(HttpMessageNotReadableException e) {
        log.error(e.getMessage(), e);
        return Result.error(400, e.getMessage());
    }

    //400错误
    @ExceptionHandler({NoHandlerFoundException.class})
    public Result requestNotReadable(NoHandlerFoundException e) {
        log.error(e.getMessage(), e);
        return Result.error(400, e.getMessage());
    }

    @ExceptionHandler({MethodArgumentNotValidException.class})
    public Result requestNotReadable(MethodArgumentNotValidException e) {
        log.error(e.getMessage(), e);
        return Result.error(400, e.getMessage());
    }


    @ExceptionHandler(RuntimeException.class)
    public Result runtimeExceptionHandler(RuntimeException e) {
        return Result.error(500, e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(Exception e) {
        return Result.error(500, e.getMessage());
    }


}
